package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.OrderService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.OrderDto;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lizhiquan
 * @CreateDate: 2021-04-02-15-31
 * @Description:
 */
@RestController
@RequestMapping("/api/order")
public class OrderController extends BaseController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    @PostMapping("/list")
    public ServerResponse list(HttpServletRequest request, @RequestBody OrderDto orderDto) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        UserInfo userInfo = userService.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        orderDto.setPageNum(Integer.parseInt(pageNum));
        orderDto.setPageSize(Integer.parseInt(pageSize));
        return ServerResponse.createBySuccess(orderService.findOrderList(orderDto));
    }

    @PostMapping("/export/list")
    public ServerResponse exportList(HttpServletRequest request, @RequestBody OrderDto orderDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        if(orderDto.getCreateTime()==null){
            return ServerResponse.createByErrorMessage("请求异常，导出时间间隔建议在一个月内");
        }
        boolean flag = compareReqTimeSectionMonth(orderDto.getCreateTime(),orderDto.getUpdateTime());
        if(!flag){
            return ServerResponse.createByErrorMessage("请求异常，导出时间间隔建议在一个月内");
        }
        return orderService.exportList(orderDto);
    }

    @PostMapping("/findOrderStatistics")
    public ServerResponse findOrderStatistics(HttpServletRequest request, @RequestBody OrderDto orderDto) throws Exception {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        return orderService.findOrderStatistics(orderDto);
    }

    private boolean compareReqTimeSectionMonth(Date beginTime, Date endTime) {
        Calendar calendar = Calendar.getInstance(); // 得到日历
        if (beginTime == null) {
            return false;
        }
        calendar.setTime(beginTime); // 把当前时间赋给日历
        calendar.add(Calendar.MONTH, 1); // 添加一个月
        beginTime = calendar.getTime();
        int i = beginTime.compareTo(endTime);
        return i >= 0;
    }

}
